﻿
using NP.BPMReportPlatform.AUTO;
using NP.BPMReportPlatform.AUTO.Adjust.Dto;
using NP.BPMReportPlatform.AUTO.Quotation.Dto;
using NP.BPMReportPlatform.AUTO.TechnicalDependence.Dto;
using NP.BPMReportPlatform.BPMPlatform;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Engine;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.CommonEnum;
using NP.BPMReportPlatform.FlowManagement.Dto;
using NP.BPMReportPlatform.IC.ICZHYLS.Dto;
using NP.BPMReportPlatform.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.IC
{
    public class ICZHYLSAppService : BPMReportPlatformAppService, IICZHYLSAppService
    {
        private readonly SqlSugarClient _db, _dbEngine;
        private readonly SqlSugarClient _dbPlatform;
        private readonly IBPMUtilsAppService _bPMUtilsApp;
        private readonly IBPMPlatformAppService _iBPMPlatformApp;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlSugarAppService"></param>
        public ICZHYLSAppService(ISqlSugarAppService sqlSugarAppService, IBPMUtilsAppService bPMUtilsApp, IBPMPlatformAppService iBPMPlatformApp)
        {
            _db = sqlSugarAppService.GetBusinessInstance();
            _dbEngine = sqlSugarAppService.GetEngineInstance();
            _dbPlatform = sqlSugarAppService.GetPlatformInstance();
            _bPMUtilsApp = bPMUtilsApp;
            _iBPMPlatformApp = iBPMPlatformApp;
        }

        #region IC综合依赖书报表
        /// <summary>
        /// 获取Queryable
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private ISugarQueryable<ICZHYLSOutput> GetQueryable(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }

            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail2>(
                    (x, bu, d2) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId, JoinType.Left, x.InstanceNumber == d2.InstanceNumber))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber),
                    (x, bu) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductCode1), (x, bu, d2) => d2.ProductCode1.Contains(input.ProductCode1))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)

                .Select((x, bu, d2) =>
                    new ICZHYLSOutput()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        ProjcetInfo = x.ProjcetInfo,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        ProductCode1 = d2.ProductCode1,


                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString(),
                        UserName = bu.StartUserName,
                        //SyncStatus = m.Status,
                        //ApproverName = x.ApproverName,
                        //ApproveTime = x.ApproveTime,
                        //XSXZZYName = x.XSXZZYName,
                        //SettlementTotal = x.SettlementTotal,
                        //SettlementAdjustTotal = x.SettlementAdjustTotal,
                        //TotalRMCRatio = x.TotalRMCRatio,
                        //TotalRMC2Ratio = x.TotalRMC2Ratio,
                        //TotalGrossProfit1 = x.TotalGrossProfit1,
                        //TotalGrossProfit2 = x.TotalGrossProfit2,
                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }

 




        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<ICZHYLSOutput>> GetAllAsync(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryable(input);
            var listViewDto = await listQueryable.ToPageListAsync(input.PageIndex, input.PageSize, totalCount);

            var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);
            listViewDto.ForEach(x =>
            {
                x.Url = _bPMUtilsApp.GetProcessUrl(x.InstanceNumber, userId);

            });



            //返回结果
            return new PagedResultDto<ICZHYLSOutput>()
            {
                TotalCount = totalCount,
                Items = listViewDto
            };
        }


 

        #endregion

        private string ConvertToQueryStatus(TaskCommonStatus status, bool isInnerState = false)
        {
            string queryStatus;

            switch (status)
            {
                case TaskCommonStatus.RecallStart:
                case TaskCommonStatus.Start:
                case TaskCommonStatus.ReStart:
                case TaskCommonStatus.RejectStart:
                case TaskCommonStatus.RejectStartDirect:
                case TaskCommonStatus.RecallActivity:
                    queryStatus = "待发起";
                    break;
                case TaskCommonStatus.Processing:
                    queryStatus = "处理中";
                    break;
                case TaskCommonStatus.RejectActivity:
                case TaskCommonStatus.RejectActivityDirect:
                case TaskCommonStatus.Handover:
                case TaskCommonStatus.ExtraAppend:
                case TaskCommonStatus.ExtraInsert:
                case TaskCommonStatus.SkippedWhenEmptyResolver:
                case TaskCommonStatus.SkippedWhenSameApprover:
                    queryStatus = isInnerState ? "已完成" : "处理中";
                    break;
                case TaskCommonStatus.Done:
                    queryStatus = "通过";
                    break;
                case TaskCommonStatus.Refused:
                    queryStatus = "拒绝";
                    break;
                case TaskCommonStatus.Canceled:
                case TaskCommonStatus.InterveneAddNode:
                case TaskCommonStatus.InterveneJumpNode:
                case TaskCommonStatus.InterveneChangeUsers:
                case TaskCommonStatus.InterveneReorder:
                case TaskCommonStatus.InterveneRemoveNode:
                    queryStatus = "终止";
                    break;
                case TaskCommonStatus.Exception:
                    queryStatus = "有异常";
                    break;
                default:
                    queryStatus = "待发起";
                    break;
            }

            return queryStatus;
        }

        /// <summary>
        /// 状态名称转化成状态编码
        /// </summary>
        /// <param name="status"></param>
        /// <param name="isInnerState"></param>
        /// <returns></returns>
        private string ConvertToQueryStatusCode(string statusNme)
        {
            string queryStatusCode;

            switch (statusNme)
            {
                case "处理中":
                    queryStatusCode = "Processing";
                    break;
                case "通过":
                    queryStatusCode = "Approved";
                    break;
                case "终止":
                    queryStatusCode = "Canceled";
                    break;
                case "拒绝":
                    queryStatusCode = "Refused";
                    break;
                default:
                    queryStatusCode = "";
                    break;
            }

            return queryStatusCode;
        }


        #region IC综合依赖书报表导出GetQueryableExport1

        private ISugarQueryable<ExportICZHYLSOutput1> GetQueryableExport1(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }

            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail>(
                    (x, bu, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)

                .Select((x, bu, d1) =>
                    new ExportICZHYLSOutput1()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                        OutSiteSupportType = x.OutSiteSupportType,
                        YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                        Jjcd = x.Jjcd,
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                        YlsCreateTime = x.YlsCreateTime.Value.ToString("yyyy-MM-dd"),
                        XcqhServerDesc = x.XcqhServerDesc,
                        Sjddkhxcrq = x.Sjddkhxcrq.Value.ToString("yyyy-MM-dd"),
                        Wcrq = x.Wcrq.Value.ToString("yyyy-MM-dd"),
                        ProductCode1 = d1.ProductCode,
                        ProductName1 = d1.ProductName,
                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }

        private ISugarQueryable<ExportICZHYLSOutput21> GetQueryableExport21(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail>(
                    (x, bu, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)

                .Select((x, bu, d1) =>
                    new ExportICZHYLSOutput21()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                        QwtjTime = x.QwtjTime.Value.ToString("yyyy-MM-dd"),
                        YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                        Jjcd = x.Jjcd,
                        YjAddress = x.YjAddress,
                        YjSJR = x.YjSJR,
                        YjPhone = x.YjPhone,
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                        Sjddkhxcrq = x.Sjddkhxcrq.Value.ToString("yyyy-MM-dd"),
                        Wcrq = x.Wcrq.Value.ToString("yyyy-MM-dd"),
                        Jszllx = d1.Jszllx,
                        Sfxyyjjszyj = d1.Sfxyyjjszyj,
                        ProductCode1 = d1.ProductCode,
                        ProductName1 = d1.ProductName,
                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                        // //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }


        private ISugarQueryable<ExportICZHYLSOutput3> GetQueryableExport3(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }

            var listQueryable1 = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail, ICSalesTechDocDetail2>(
                    (x, bu, d1, d2) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Inner, x.InstanceNumber == d1.InstanceNumber,
                    JoinType.Left, d1.InstanceNumber == d2.InstanceNumber && d1.ModifyBy==d2.ModifyBy1))

                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1, d2) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductCode1), (x, bu, d1, d2) => d2.ProductCode1.Contains(input.ProductCode1))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)

                .Select((x, bu, d1, d2) =>
                    new ExportICZHYLSOutput3()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                        Bzbtgqk = x.Bzbtgqk,
                        BzbtgNum = x.BzbtgNum,
                        Bzbtgdbyq = x.Bzbtgdbyq,
                        Bzbtgybcc = x.Bzbtgybcc,
                        Sfyhb = x.Sfyhb,
                        Sfysyxq = x.Sfysyxq,
                        Twyq = x.Twyq,
                        Ydyq = x.Ydyq,
                        Ybsfptdq = x.Ybsfptdq,
                        Dqcpmchys = x.Dqcpmchys,
                        ybmhyq = x.ybmhyq,
                        Ybmhyq1 = x.Ybmhyq1,
                        Hkwd = x.Hkwd,
                        Tsjcyq = x.Tsjcyq,
                        gzyq1 = x.gzyq1,
                        Gzyq2 = x.Gzyq2,
                        Gzyq3 = x.Gzyq3,
                        Khtxrzyq = x.Khtxrzyq,
                        Tshktj = x.Tshktj,
                        Khhbrzyq = x.Khhbrzyq,
                        Cpjzxx = x.Cpjzxx,
                        Qtgyyq = x.Qtgyyq,
                        ApproveJSTS = x.ApproveJSTS,
                        YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                        Jjcd = x.Jjcd,
                        YjAddress = x.YjAddress,
                        YjSJR = x.YjSJR,
                        YjPhone = x.YjPhone,
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                        YlsCreateTime = x.YlsCreateTime.Value.ToString("yyyy-MM-dd"),
                        XcqhServerDesc = x.XcqhServerDesc,
                        Sjddkhxcrq = x.Sjddkhxcrq.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                        Wcrq = x.Wcrq.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                        Xcpmc = x.Xcpmc,
                        Fjdh = x.Fjdh,
                        ClientType = x.ClientType,

                        ModifyBy = d1.ModifyBy,
                        ProductCode = d1.ProductCode,
                        ProductName = d1.ProductName,
                        Sfxs = d1.Sfxs,
                        Desc = d1.Desc,
                        ProductZL = d1.ProductZL,
                        ProductNum = d1.ProductNum,

                        ModifyBy1 = d2.ModifyBy1,
                        UsersName1 = d2.UsersName1,
                        ProductCode1 = d2.ProductCode1,
                        ProductName1 = d2.ProductName1,
                        ProductZL1 = d2.ProductZL1,
                        ProductNum1 = d2.ProductNum1,
                        Sfxs1 = d2.Sfxs1,
                        Czrq1 = d2.Czrq1.Value.ToString("yyyy-MM-dd"),

                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    });


            var listQueryable2 = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail2, ICSalesTechDocDetail >(
                  (x, bu, d2, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                  JoinType.Inner, x.InstanceNumber == d2.InstanceNumber,
                  JoinType.Left, d2.InstanceNumber == d1.InstanceNumber && d2.ModifyBy1 == d1.ModifyBy))

              .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
              .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d2 ,d1) => x.InstanceNumber.Contains(input.InstanceNumber))
              .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
              .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
              .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
              .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
              .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
              .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
              .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
              .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
              .WhereIF(!string.IsNullOrWhiteSpace(input.ProductCode1), (x, bu, d2, d1) => d2.ProductCode1.Contains(input.ProductCode1))
              .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)

              .Select((x, bu,  d2,d1) =>
                  new ExportICZHYLSOutput3()
                  {
                      InstanceNumber = x.InstanceNumber,
                      Factory = x.Factory,
                      RequirmentType = x.RequirmentType,
                      Trade1 = x.Trade1,
                      Trade2 = x.Trade2,
                      ProductType = x.ProductType,
                      CustomerNo = x.CustomerNo,
                      CustomerName = x.CustomerName,
                      CustomGS = x.CustomGS,
                      BusinessOpportunities = x.BusinessOpportunities,
                      CustomerAddress = x.CustomerAddress,
                      CustomerPhone = x.CustomerPhone,
                      CustomType = x.CustomType,
                      TradeInfo = x.TradeInfo,
                      ZDCustomerName = x.ZDCustomerName,
                      ProjectType = x.ProjectType,
                      LbLastXSJE = x.LbLastXSJE,
                      LbLastTLCGZJE = x.LbLastTLCGZJE,
                      ProYGTLNCGJE = x.ProYGTLNCGJE,
                      ProjcetInfo = x.ProjcetInfo,
                      Bzbtgqk = x.Bzbtgqk,
                      BzbtgNum = x.BzbtgNum,
                      Bzbtgdbyq = x.Bzbtgdbyq,
                      Bzbtgybcc = x.Bzbtgybcc,
                      Sfyhb = x.Sfyhb,
                      Sfysyxq = x.Sfysyxq,
                      Twyq = x.Twyq,
                      Ydyq = x.Ydyq,
                      Ybsfptdq = x.Ybsfptdq,
                      Dqcpmchys = x.Dqcpmchys,
                      ybmhyq = x.ybmhyq,
                      Ybmhyq1 = x.Ybmhyq1,
                      Hkwd = x.Hkwd,
                      Tsjcyq = x.Tsjcyq,
                      gzyq1 = x.gzyq1,
                      Gzyq2 = x.Gzyq2,
                      Gzyq3 = x.Gzyq3,
                      Khtxrzyq = x.Khtxrzyq,
                      Tshktj = x.Tshktj,
                      Khhbrzyq = x.Khhbrzyq,
                      Cpjzxx = x.Cpjzxx,
                      Qtgyyq = x.Qtgyyq,
                      ApproveJSTS = x.ApproveJSTS,
                      YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                      Jjcd = x.Jjcd,
                      YjAddress = x.YjAddress,
                      YjSJR = x.YjSJR,
                      YjPhone = x.YjPhone,
                      CcUsersName = x.CcUsersName,
                      CPJSZYName = x.CPJSZYName,
                      YYFWZYName = x.YYFWZYName,
                      YlsCreateTime = x.YlsCreateTime.Value.ToString("yyyy-MM-dd"),
                      XcqhServerDesc = x.XcqhServerDesc,
                      Sjddkhxcrq = x.Sjddkhxcrq.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                      Wcrq = x.Wcrq.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                      Xcpmc = x.Xcpmc,
                      Fjdh = x.Fjdh,
                      ClientType = x.ClientType,

                      ModifyBy = d1.ModifyBy,
                      ProductCode = d1.ProductCode,
                      ProductName = d1.ProductName,
                      Sfxs = d1.Sfxs,
                      Desc = d1.Desc,
                      ProductZL = d1.ProductZL,
                      ProductNum = d1.ProductNum,

                      ModifyBy1 = d2.ModifyBy1,
                      UsersName1 = d2.UsersName1,
                      ProductCode1 = d2.ProductCode1,
                      ProductName1 = d2.ProductName1,
                      ProductZL1 = d2.ProductZL1,
                      ProductNum1 = d2.ProductNum1,
                      Sfxs1 = d2.Sfxs1,
                      Czrq1 = d2.Czrq1.Value.ToString("yyyy-MM-dd"),

                      OrderStatus = bu.ProcessStatusName,
                      CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                      UserName = bu.StartUserName,
                       //LatelyActivityName = bu.LatelyActivityName,
                      FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                  });

            var listQueryable = _db.UnionAll(listQueryable1, listQueryable2).Select<ExportICZHYLSOutput3>().MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }

    

        private ISugarQueryable<ExportICZHYLSOutput4> GetQueryableExport4(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail, ICSalesTechDocDetail2>(
                    (x, bu, d1, d2) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber,
                    JoinType.Left, x.InstanceNumber == d2.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1, d2) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)

                .Select((x, bu, d1, d2) =>
                    new ExportICZHYLSOutput4()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType0 = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                        Cpjzxx = x.Cpjzxx,
                        ProductCode = d1.ProductCode,
                        ProductName = d1.ProductName,
                        ProductType = d1.ProductType,
                        ProductNetWeight = d1.ProductNetWeight,
                        Packaging = d1.Packaging,
                        ProductChageReason = d1.ProductChageReason,
                        ProductCode1 = d2.ProductCode1,
                        ProductName1 = d2.ProductName1,
                        ProductType1 = d2.ProductType1,
                        ProductNetWeight1 = d2.ProductNetWeight1,
                        Packaging1 = d2.Packaging1,
                        ExpectedDate1 = d2.ExpectedDate1.Value.ToString("yyyy-MM-dd"),
                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }


        private ISugarQueryable<ExportICZHYLSOutput5> GetQueryableExport5(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail, ICSalesTechDocDetail2>(
                    (x, bu, d1, d2) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber,
                    JoinType.Left, x.InstanceNumber == d2.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1, d2) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)
                .Select((x, bu, d1, d2) =>
                    new ExportICZHYLSOutput5()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType0 = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                   
                        Cpjzxx = x.Cpjzxx,
                        SjwcTime = x.SjwcTime.Value.ToString("yyyy-MM-dd"),
                        YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                      
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                       
                        Buchongsm = x.Buchongsm,
                        Lbdypro = x.Lbdypro,
                      

                      
                        AuthType = d1.AuthType,
                        ProductCode = d1.ProductCode,
                        ProductName = d1.ProductName,
                        ProductType1 = d1.ProductType,
                        ProductPrice = d1.ProductPrice,


                        ProductCode1 = d2.ProductCode1,
                        AuthType1 = d2.AuthType1,
                        ProductName1 = d2.ProductName1,
                        ProductType2 = d2.ProductType1,
                        Packaging1 = d2.Packaging1,
         




                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }


        private ISugarQueryable<ExportICZHYLSOutput6> GetQueryableExport6(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail>(
                    (x, bu, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)
                .Select((x, bu, d1) =>
                    new ExportICZHYLSOutput6()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                       
                        YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                       
                        YjAddress = x.YjAddress,
                        YjSJR = x.YjSJR,
                        YjPhone = x.YjPhone,
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                       
                        Wcrq = x.Wcrq.Value.ToString("yyyy-MM-dd"),
                      
                        Buchongsm = x.Buchongsm,

                        ProductCode1 = d1.ProductName,
                        productSYHC = d1.productSYHC,
                        AuthType = d1.AuthType,
                      
                        ProductBL = d1.ProductBL,
                        ProductTX = d1.ProductTX,
                        ProductPC = d1.ProductPC,
                       
                        ProductSL = d1.ProductSL,
                       

                       



                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }


        private ISugarQueryable<ExportICZHYLSOutput7> GetQueryableExport7(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail>(
                    (x, bu, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)
                .Select((x, bu, d1) =>
                    new ExportICZHYLSOutput7()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                        ProjectName = d1.ProjectName,
                        ProjectDose = d1.ProjectDose,
                        ProductCode1 = d1.ProductName,
                        ProjectDesc = d1.ProjectDesc,
                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }


        private ISugarQueryable<ExportICZHYLSOutput8> GetQueryableExport8(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail>(
                    (x, bu, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)
                .Select((x, bu, d1) =>
                    new ExportICZHYLSOutput8()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                        CustomerNo = x.CustomerNo,
                        CustomerName = x.CustomerName,
                        CustomGS = x.CustomGS,
                        BusinessOpportunities = x.BusinessOpportunities,
                        CustomerAddress = x.CustomerAddress,
                        CustomerPhone = x.CustomerPhone,
                        CustomType = x.CustomType,
                        TradeInfo = x.TradeInfo,
                        ZDCustomerName = x.ZDCustomerName,
                        ProjectType = x.ProjectType,
                        LbLastXSJE = x.LbLastXSJE,
                        LbLastTLCGZJE = x.LbLastTLCGZJE,
                        ProYGTLNCGJE = x.ProYGTLNCGJE,
                        ProjcetInfo = x.ProjcetInfo,
                      
                        YwddkhxcTime = x.YwddkhxcTime.Value.ToString("yyyy-MM-dd"),
                       
                        CcUsersName = x.CcUsersName,
                        CPJSZYName = x.CPJSZYName,
                        YYFWZYName = x.YYFWZYName,
                       
                        Wcrq = x.Wcrq.Value.ToString("yyyy-MM-dd"),
                     
                        ProductBGNR = x.ProductBGNR,
                        ProductBGYY = x.ProductBGYY,
                        Cxqkms = x.Cxqkms,
                        Khsggycs = x.Khsggycs,
                        Jpbx = x.Jpbx,
                        Xmfzrlxfs = x.Xmfzrlxfs,
                        Khlxrjfs = x.Khlxrjfs,
                        Cbqwbhfw = x.Cbqwbhfw,
                        Remark = x.Remark,

                     
                        ProductCode1 = d1.ProductCode,
                        ProductName1 = d1.ProductName,
                        ProductNetWeight = d1.ProductNetWeight,
                        ProductType1 = d1.ProductType,




                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),


                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }


        private ISugarQueryable<ExportICZHYLSOutput9> GetQueryableExport9(GetICZHYLSInput input)
        {
            var currentUserId = Guid.Empty;
            if (!input.IsQueryAll)
            {
                var currentUserDto = _dbPlatform.Queryable<Users>().Where(x => x.WorkNumber.Equals(CurrentUser.UserName)).First();
                if (currentUserDto != null)
                {
                    currentUserId = currentUserDto.UserId;
                }
            }
            var listQueryable = _db.Queryable<ICSalesTechDocHeader, BusinessProcessesNew, ICSalesTechDocDetail>(
                    (x, bu, d1) => new JoinQueryInfos(JoinType.Left, x.InstanceNumber == bu.InstanceId,
                    JoinType.Left, x.InstanceNumber == d1.InstanceNumber))
                .WhereIF(!input.IsQueryAll, x => x.AddUserId.Equals(currentUserId))
                .WhereIF(!string.IsNullOrWhiteSpace(input.InstanceNumber), (x, bu, d1) => x.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(input.IsQueryAll && !string.IsNullOrWhiteSpace(input.AddUserName), x => x.AddUserName.Equals(input.AddUserName))
                .WhereIF(!input.OrderStatus.IsNullOrEmpty(), (x, bu) => SqlFunc.ContainsArray<string>(input.OrderStatus.ToArray(), bu.ProcessStatusName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.RequirmentType), x => x.RequirmentType.Equals(input.RequirmentType))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Factory), x => x.Factory.Equals(input.Factory))
                .WhereIF(!string.IsNullOrWhiteSpace(input.Trade1), x => x.Trade1.Equals(input.Trade1))
                .WhereIF(!string.IsNullOrWhiteSpace(input.CustomerName), x => x.CustomerName.Contains(input.CustomerName))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProjcetInfo), x => x.ProjcetInfo.Contains(input.ProjcetInfo))
                .WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), x => x.ProductType.Contains(input.ProductType))
                .WhereIF(input.StartCreationTime != null && input.EndCreationTime != null, x => x.AddTime >= input.StartCreationTime && x.AddTime.AddDays(-1) <= input.EndCreationTime)
                .Select((x, bu, d1) =>
                    new ExportICZHYLSOutput9()
                    {
                        InstanceNumber = x.InstanceNumber,
                        Factory = x.Factory,
                        RequirmentType = x.RequirmentType,
                        Trade1 = x.Trade1,
                        Trade2 = x.Trade2,
                        ProductType = x.ProductType,
                     
                      

                        ModifyBy = d1.ModifyBy,
                        ProductCode1 = d1.ProductCode,
                        ProductName1 = d1.ProductName,
                        Sfxs = d1.Sfxs,
                        Desc = d1.Desc,
                        ProductZL = d1.ProductZL,
                        ProductNum = d1.ProductNum,


                        OrderStatus = bu.ProcessStatusName,
                        CreateTime = bu.StartTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        UserName = bu.StartUserName,
                         //LatelyActivityName = bu.LatelyActivityName,
                        FinishTime = bu.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"),



                    }).MergeTable().Distinct().OrderBy(ins => ins.InstanceNumber, OrderByType.Desc);

            return listQueryable;
        }
        #endregion

        #region IC综合依赖书报表导出Export1Async
        /// <summary>
        /// 报表导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<ExportICZHYLSOutput1>> Export1Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport1(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);



            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }

        public async Task<List<ExportICZHYLSOutput21>> Export21Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport21(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);



            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }


        public async Task<List<ExportICZHYLSOutput3>> Export3Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport3(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }

        //public  DataTable Export31Async(GetICZHYLSInput input)
        //{
        //    //await UpdateOrderStatus(input);
        //    RefAsync<int> totalCount = 0;
        //    var listQueryable = GetQueryableExport31(input);
        //    //var listViewDto = await listQueryable.AsDataView();
        //    //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


        //    //var i = 1;
        //    //listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
        //    return listQueryable;
        //}

        public async Task<List<ExportICZHYLSOutput4>> Export4Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport4(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);




            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }

        public async Task<List<ExportICZHYLSOutput5>> Export5Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport5(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }

        public async Task<List<ExportICZHYLSOutput6>> Export6Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport6(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }

        public async Task<List<ExportICZHYLSOutput7>> Export7Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport7(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }

        public async Task<List<ExportICZHYLSOutput8>> Export8Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport8(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }


        public async Task<List<ExportICZHYLSOutput9>> Export9Async(GetICZHYLSInput input)
        {
            //await UpdateOrderStatus(input);
            RefAsync<int> totalCount = 0;
            var listQueryable = GetQueryableExport9(input);
            var listViewDto = await listQueryable.ToListAsync();
            //var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);


            var i = 1;
            listViewDto.ForEach(x => x.SerialNumber = i++.ToString());
            return listViewDto;
        }



        #endregion
    }
}
